﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLTB.Data;
using QLTB.Entities;
using _4rumDCT.App_Code;

namespace QuanLyThietBi.DCTEDU_.QuanTri
{
    public partial class CNThoiKhoaBieu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCatalog.BindPhongHoc(cboPhong);
                if (Request.QueryString["id"] != null)
                {
                    H_CatalogId.Value = Request.QueryString["id"].ToString();
                    BindData1(Convert.ToInt32(H_CatalogId.Value));
                }
            }
        }

        protected void BindData1(int Id)
        {
            ThoiKhoaBieu ent_TKB = DataRepository.ThoiKhoaBieuProvider.GetByMaTkb(Id);
            txtMaLopHP.Text = ent_TKB.MaLopHp;
            txtThoiGian.Text = ent_TKB.ThoiGianBatDau.ToString("dd/MM/yyyy");
            txtTuTiet.Text = ent_TKB.TuTiet.ToString();
            txtDenTiet.Text = ent_TKB.DenTiet.ToString();
            cboPhong.SelectedValue = ent_TKB.MaPhong.ToString();
        }

        protected void cmdBack_OnClick(object sender, EventArgs e)
        {
            Response.Redirect("~/DCTEDU_/QuanTri/QLThoiKhoaBieu.aspx");
        }
        protected void cmdSave_OnClick(object sender, EventArgs e)
        {
            bool updateStatus = false;
            try
            {
                if (DataRepository.LopHocPhanProvider.GetByMaLopHp(txtMaLopHP.Text) == null)
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constant.THATBAI, Constant.LOPHOCPHAN_KHONGTONTAI, true);
                    return;
                }
                ThoiKhoaBieu ent_TKB = new ThoiKhoaBieu();
                ent_TKB.MaTkb = Convert.ToInt32(H_CatalogId.Value);
                ent_TKB.TuTiet = Convert.ToInt32(txtTuTiet.Text);
                ent_TKB.DenTiet = Convert.ToInt32(txtDenTiet.Text);
                ent_TKB.MaPhong = Convert.ToInt32(cboPhong.SelectedValue);
                ent_TKB.MaLopHp = txtMaLopHP.Text;
                ent_TKB.ThoiGianBatDau = DateTime.ParseExact(Common.StandardString(txtThoiGian.Text), "dd/MM/yyyy", CultureInfo.InvariantCulture);
                if (ent_TKB.MaTkb == 0)
                {
                    updateStatus = DataRepository.ThoiKhoaBieuProvider.Insert(ent_TKB);
                }
                else
                {
                    ent_TKB.EntityState = EntityState.Changed;
                    updateStatus = DataRepository.ThoiKhoaBieuProvider.Update(ent_TKB);
                }
                if (!updateStatus)
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constant.THATBAI, Constant.CAPNHAT_THATBAI, true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constant.THANHCONG, Constant.CAPNHAT_THANHCONG, true);
                    RedirectTo("~/DCTEDU_/QuanTri/QLThoiKhoaBieu.aspx");
                    H_CatalogId.Value = ent_TKB.MaTkb.ToString();
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("CNThoiKhoaBieu.aspx.cs", "cmdLuu_Click", ex.ToString());
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constant.THATBAI, Constant.CAPNHAT_THATBAI, true);
            }
        }
        private void RedirectTo(string url)
        {
            string redirectURL = Page.ResolveClientUrl(url);
            string script = String.Format("window.location = '{0}';", redirectURL);
            ScriptManager.RegisterStartupScript(this, typeof(Page), "RedirectTo", script, true);
        }

        protected void cmdTuUp_OnClick(object sender, EventArgs e)
        {
            txtTuTiet.Text = (Convert.ToInt32(txtTuTiet.Text) + 1).ToString();
            txtDenTiet.Text = (Convert.ToInt32(txtTuTiet.Text) + 1).ToString();
        }

        protected void cmdTuDown_OnClick(object sender, EventArgs e)
        {
            if (Convert.ToInt32(txtTuTiet.Text) == 0) return;
            txtTuTiet.Text = (Convert.ToInt32(txtTuTiet.Text) - 1).ToString();
            txtDenTiet.Text = (Convert.ToInt32(txtTuTiet.Text) + 1).ToString();
        }

        protected void cmdDenUp_OnClick(object sender, EventArgs e)
        {

            txtDenTiet.Text = (Convert.ToInt32(txtDenTiet.Text) + 1).ToString();

        }

        protected void cmdDenDown_OnClick(object sender, EventArgs e)
        {
            if (Convert.ToInt32(txtDenTiet.Text) == 0) return;
            if (Convert.ToInt32(txtDenTiet.Text) > Convert.ToInt32(txtTuTiet.Text))
            {
                txtDenTiet.Text = (Convert.ToInt32(txtDenTiet.Text) - 1).ToString();
            }
            else
            {
                return;
            }
        }
    }
}